﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.Dao.DataFilters;
using Vladsm.Web.UI.WebControls;

namespace AB.ServiceDirectory.Design.WebUserControls.Lookup
{
    public partial class AddressLookup : UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var filter = new AddressFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Buttons the search click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as AddressFilter;
            if (filter != null)
            {
                filter.Postcode = TextBoxPostcode.Text;
                filter.Street = TextBoxStreet.Text;
                filter.Town = TextBoxTown.Text;
            }
            GridViewSearchPostcode.DataBind();
        }

        /// <summary>
        /// Buttons the clear click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonClearClick(object sender, EventArgs e)
        {
            TextBoxTown.Text = string.Empty;
            TextBoxStreet.Text = string.Empty;
            TextBoxPostcode.Text = string.Empty;
        }

        /// <summary>
        /// Buttons the none click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonNoneClick(object sender, EventArgs e)
        {
            GridViewSearchPostcode.DataSource = null;
            GridViewSearchPostcode.DataBind();
        }

        /// <summary>
        /// Buttons the close click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonCloseClick(object sender, EventArgs e)
        {
            Response.Redirect("~/Index.aspx");
        }

        /// <summary>
        /// Buttons the select click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSelectClick(object sender, EventArgs e)
        {
            foreach (GridViewRow postcodeGridView in GridViewSearchPostcode.Rows)
            {
                if (postcodeGridView.RowType == DataControlRowType.DataRow)
                {
                    var grd = (GroupRadioButton)postcodeGridView.FindControl("GroupRadioButton1");
                    if (grd != null && grd.Checked)
                    {
                        DataKey dataKey = GridViewSearchPostcode.DataKeys[postcodeGridView.RowIndex];
                        if (postcodeGridView.RowIndex < 0)
                        {
                            return;
                        }
                        if (dataKey != null)
                        {
                            var key = (int)dataKey.Value;
                            //TextBoxTown.Text = key.ToString();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Addresses the object data source selecting.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void AddressObjectDataSourceSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            var filter = ViewState["filter"] as AddressFilter;
            e.InputParameters["filter"] = filter;
        }

        public void Popup()
        {
            PanelContainer.CssClass = "popup_container";
            PanelContainer.Visible = true;
        }
    }
}